package com.example.voicedet;




import com.example.voicedet.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.preference.UserPreference;;


public class MainActivity extends Activity implements OnClickListener{

	private boolean recog_flag = true;//语音识别启动    标志位
	private boolean float_flag = true;//语音识别 悬浮图标   显示标志位
	
	private RelativeLayout rl_switch_voice_detect;
	private RelativeLayout rl_switch_float_window;
	
	private ImageView iv_switch_open_voice_detect;
	private ImageView iv_switch_close_voice_detect;
	private ImageView iv_switch_open_float_window;
	private ImageView iv_switch_close_float_window;
	
	private UserPreference mUserPreference;

	private boolean mEnableFloatMic;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mUserPreference = new UserPreference(this);
		
		rl_switch_voice_detect = (RelativeLayout) findViewById(R.id.rl_switch_voice_detect);
		rl_switch_float_window = (RelativeLayout) findViewById(R.id.rl_switch_float_window);

		iv_switch_open_voice_detect  = (ImageView) findViewById(R.id.iv_switch_open_voice_detect);
		iv_switch_close_voice_detect = (ImageView) findViewById(R.id.iv_switch_close_voice_detect);
		iv_switch_open_float_window  = (ImageView) findViewById(R.id.iv_switch_open_float_window);
		iv_switch_close_float_window = (ImageView) findViewById(R.id.iv_switch_close_float_window);
		
		rl_switch_voice_detect.setOnClickListener(this);
		rl_switch_float_window.setOnClickListener(this);
		
		/*iv_switch_open_voice_detect.setOnClickListener(this);
		iv_switch_close_voice_detect.setOnClickListener(this);
		iv_switch_open_float_window.setOnClickListener(this);
		iv_switch_close_float_window.setOnClickListener(this);*/
		
		
		registerListener();
		
		Intent intent = new Intent(MainActivity.this,WindowService.class);  
		startService(intent);
		
		float_flag = mUserPreference.getBoolean(UserPreference.KEY_ENABLE_FLOATSHOW, true);
		recog_flag = mUserPreference.getBoolean(UserPreference.KEY_ENABLE_WAKEUP, true);
		
		if(float_flag)
		{
			iv_switch_open_float_window.setVisibility(View.VISIBLE);
			iv_switch_close_float_window.setVisibility(View.INVISIBLE);
		}
		else
		{
			iv_switch_open_float_window.setVisibility(View.INVISIBLE);
			iv_switch_close_float_window.setVisibility(View.VISIBLE);
		}
        
		if(recog_flag)
		{
			iv_switch_open_voice_detect.setVisibility(View.VISIBLE);
			iv_switch_close_voice_detect.setVisibility(View.INVISIBLE);
		}
		else
		{
			iv_switch_open_voice_detect.setVisibility(View.INVISIBLE);
			iv_switch_close_voice_detect.setVisibility(View.VISIBLE);
		}

	}
	
	public void onClick(View v){
		Log.d("voice","switch");
		
//		Intent i = new Intent(MainActivity.this, WindowService.class);
		switch (v.getId()) {
			/*case R.id.iv_switch_open_voice_detect://关闭语音识别
				Log.d("voice","voice_detect");
				iv_switch_open_voice_detect.setVisibility(View.INVISIBLE);
				iv_switch_close_voice_detect.setVisibility(View.VISIBLE);
				mUserPreference.putBoolean(UserPreference.KEY_ENABLE_WAKEUP, false);
				break;
				
			case R.id.iv_switch_close_voice_detect://开启语音识别
				Log.d("voice","voice_detect");
				iv_switch_open_voice_detect.setVisibility(View.VISIBLE);
				iv_switch_close_voice_detect.setVisibility(View.INVISIBLE);
				mUserPreference.putBoolean(UserPreference.KEY_ENABLE_WAKEUP, true);
				break;
					
			case R.id.iv_switch_open_float_window://关闭悬浮窗口
				Log.d("voice","float_window");
				iv_switch_open_float_window.setVisibility(View.INVISIBLE);
				iv_switch_close_float_window.setVisibility(View.VISIBLE);
				mUserPreference.putBoolean(UserPreference.KEY_ENABLE_FLOATSHOW, false);
				break;
				
			case R.id.iv_switch_close_float_window://开启悬浮窗口
				Log.d("voice","float_window");
				iv_switch_open_float_window.setVisibility(View.VISIBLE);
				iv_switch_close_float_window.setVisibility(View.INVISIBLE);
				mUserPreference.putBoolean(UserPreference.KEY_ENABLE_FLOATSHOW, true);
				break;*/
			case R.id.rl_switch_voice_detect:
				if(iv_switch_open_voice_detect.getVisibility() == View.VISIBLE){
					iv_switch_open_voice_detect .setVisibility(View.INVISIBLE);
					iv_switch_close_voice_detect .setVisibility(View.VISIBLE);
					mUserPreference.putBoolean(UserPreference.KEY_ENABLE_WAKEUP, false);
				}else{
					iv_switch_open_voice_detect .setVisibility(View.VISIBLE);
					iv_switch_close_voice_detect .setVisibility(View.INVISIBLE);
					mUserPreference.putBoolean(UserPreference.KEY_ENABLE_WAKEUP, true);
				}
				break;
			case R.id.rl_switch_float_window:
				if(iv_switch_open_float_window.getVisibility() == View.VISIBLE){
					iv_switch_open_float_window .setVisibility(View.INVISIBLE);
					iv_switch_close_float_window .setVisibility(View.VISIBLE);
					mUserPreference.putBoolean(UserPreference.KEY_ENABLE_FLOATSHOW, false);
			}else{
					iv_switch_open_float_window .setVisibility(View.VISIBLE);
					iv_switch_close_float_window .setVisibility(View.INVISIBLE);
					mUserPreference.putBoolean(UserPreference.KEY_ENABLE_FLOATSHOW, true);
				}
				
			default:
				break;
				
		}
	}

	
	private OnSharedPreferenceChangeListener mPreferenceChangeListener = new OnSharedPreferenceChangeListener() {

		@Override
		public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
			Log.d("PreferenceChange", "onSharedPreferenceChanged: key " + key);
			if(key.equals(UserPreference.KEY_ENABLE_FLOATSHOW))
			{
				if(mUserPreference.getBoolean(UserPreference.KEY_ENABLE_FLOATSHOW, true))
				{
					iv_switch_open_float_window.setVisibility(View.VISIBLE);
					iv_switch_close_float_window.setVisibility(View.INVISIBLE);
				}
				else
				{
					iv_switch_open_float_window.setVisibility(View.INVISIBLE);
					iv_switch_close_float_window.setVisibility(View.VISIBLE);
				}
			}
			
			if(key.equals(UserPreference.KEY_ENABLE_WAKEUP))
			{
				if(mUserPreference.getBoolean(UserPreference.KEY_ENABLE_WAKEUP, true))
				{

				}
				else
				{

				}
			}
		}
	};
	
	private void registerListener() {
		mUserPreference.registerOnSharedPreferenceChangeListener(mPreferenceChangeListener);
	}
	
	private void unregisterListener() {
		mUserPreference.unregisterOnSharedPreferenceChangeListener(mPreferenceChangeListener);
	}
	

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.main, menu);
//		return true;
//	}

//	@Override
//	public boolean onOptionsItemSelected(MenuItem item) {
//		// Handle action bar item clicks here. The action bar will
//		// automatically handle clicks on the Home/Up button, so long
//		// as you specify a parent activity in AndroidManifest.xml.
//		int id = item.getItemId();
//		if (id == R.id.action_settings) {
//			return true;
//		}
//		return super.onOptionsItemSelected(item);
//	}
}
